package ch.ethz.fcl.metrobuzz.data.interchange;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import ch.ethz.fcl.metrobuzz.data.MBData;
import ch.ethz.fcl.metrobuzz.data.singapore.SGInterchange;

/**
 * read region information from files
 * 
 * @author Zeng Wei
 * @email zeng@arch.ethz.ch
 * 
 */
public class RegionReader {
	private MBData data;

	public RegionReader(MBData data) {
		this.data = data;
	}

	@SuppressWarnings("resource")
	public ArrayList<Region> readRegionInfo(String fileName, int level) {
		ArrayList<Region> regions = new ArrayList<Region>();

		FileReader fr;
		try {
			fr = new FileReader(fileName);
			BufferedReader br = new BufferedReader(fr);
			String s = null;
			while ((s = br.readLine()) != null) {
				String[] subs = s.split("\t");
				int index = Integer.parseInt(subs[1]);
				float radius = Float.parseFloat(subs[3]);
				int colorIndex = Integer.parseInt(subs[5]);
				int zoneIndex = Integer.parseInt(subs[7]);
				float x = 0;
				float y = 0;
				if (subs.length > 9) {
					x = Float.parseFloat(subs[9]);
					y = Float.parseFloat(subs[11]);
				}
				Region region = new Region(index);
				region.setRadius(radius);
				region.setColorIndex(colorIndex);
				region.setZoneIndex(data, zoneIndex, level);
				if (x != 0 && y != 0)
					region.setCentroid(x, y);

				s = br.readLine();
				subs = s.split("\t");

				for (int i = 1; i < subs.length; i++)
					region.addConnectedInterchangeIndex(Integer.parseInt(subs[i]));

				s = br.readLine();
				subs = s.split("\t");

				for (int i = 1; i < subs.length; i++)
					region.addConnectedInterchangeColor(i - 1, Integer.parseInt(subs[i]));

				regions.add(region);
				br.readLine();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return regions;
	}

	@SuppressWarnings("resource")
	public void readRegionDistribution(SGInterchange interchange,
			String file_name, int level) {
		FileReader fr;
		try {
			fr = new FileReader(file_name);
			BufferedReader br = new BufferedReader(fr);
			String s = null;
			int index = 0;
			while ((s = br.readLine()) != null) {
				if (s.startsWith("index")) {
					String[] subs = s.split("\t");
					index = Integer.parseInt(subs[1]);
				} else {
					Region region = interchange.getRegions(level).get(index);
					String[] subs = s.split("\t");
					String[] fromto = subs[0].split(":");
					int from = Integer.parseInt(fromto[0]);
					int to = Integer.parseInt(fromto[1]);

					for (int i = 1; i < subs.length; i++) {
						String[] timesize = subs[i].split(":");
						int time = Integer.parseInt(timesize[0]);
						int size = Integer.parseInt(timesize[1]);

						if (region.distribution == null)
							region.distribution = new int[region.matrixSize][region.matrixSize][MBData.ONE_DAY_TIME];

						region.distribution[from][to][time] = size;
						// region.setDistribution(from, to, time, size);
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		for (int i = 0; i < interchange.getRegions(level).size(); i++)
			interchange.getRegions(level).get(i).initCircos();
	}
}
